import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getwidget/getwidget.dart';
import 'package:meal/constant/fonts.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/route/route.dart';
import 'package:meal/ui/draw_under_line.dart';
import 'package:meal/ui/scroll_refresh_config.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class GroupOrder extends StatefulWidget {
  const GroupOrder({Key? key}) : super(key: key);

  @override
  State<GroupOrder> createState() => _GroupOrderState();
}

class _GroupOrderState extends State<GroupOrder> {
  late RefreshController refreshController;

  List<_GroupOrderModel> pageList = [];

  @override
  void initState() {
    super.initState();
    refreshController = RefreshController(initialRefresh: false);
    pageList.add(_GroupOrderModel(
        orderStatus: '未付款',
        mobile: '135998476'.replaceRange(3, 8, '*'),
        meetPay: 875.04,
        income: 670.01,
        children: [
          _OrderInfoModel(
              imgUrl:
                  'https://img1.baidu.com/it/u=3033226715,2238019049&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
              name: '烤肉两人套餐烤肉两人套餐烤肉两人套餐烤肉两人套餐')
        ]));

    pageList.add(_GroupOrderModel(
        orderStatus: '未使用',
        mobile: '135998476'.replaceRange(3, 8, '*'),
        meetPay: 875.04,
        income: 670.01,
        children: [
          _OrderInfoModel(
              imgUrl:
                  'https://img1.baidu.com/it/u=3033226715,2238019049&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
              name: '烤肉两人套餐烤肉两人套餐烤肉两人套餐烤肉两人套餐',
              note: '剩1份 · 有效期至2022-06-08'),
          _OrderInfoModel(
              imgUrl:
                  'https://img1.baidu.com/it/u=3033226715,2238019049&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
              name: '烤肉两人套餐烤肉两人套餐烤肉两人套餐烤肉两人套餐',
              statusText: '退款中'),
        ]));

    pageList.add(_GroupOrderModel(
        orderStatus: '已使用',
        mobile: '135998476'.replaceRange(3, 8, '*'),
        meetPay: 875.04,
        income: 670.01,
        children: [
          _OrderInfoModel(
              imgUrl:
                  'https://img1.baidu.com/it/u=3033226715,2238019049&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
              name: '烤肉两人套餐烤肉两人套餐烤肉两人套餐烤肉两人套餐')
        ]));

    pageList.add(_GroupOrderModel(
        orderStatus: '关闭交易',
        mobile: '135998476'.replaceRange(3, 8, '*'),
        meetPay: 875.04,
        income: 670.01,
        children: [
          _OrderInfoModel(
              imgUrl:
                  'https://img1.baidu.com/it/u=3033226715,2238019049&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
              name: '烤肉两人套餐烤肉两人套餐烤肉两人套餐烤肉两人套餐')
        ]));

    pageList.add(_GroupOrderModel(
        orderStatus: '关闭交易',
        mobile: '135998476'.replaceRange(3, 8, '*'),
        meetPay: 875.04,
        income: 670.01,
        children: [
          _OrderInfoModel(
              imgUrl:
                  'https://img1.baidu.com/it/u=3033226715,2238019049&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
              name: '烤肉两人套餐烤肉两人套餐烤肉两人套餐烤肉两人套餐',
            statusText: '退款成功'
          )
        ]));
  }

  @override
  void dispose() {
    refreshController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: refreshController,
      onRefresh: onRefresh,
      onLoading: onLoading,
      child: listViewBuilder(),
    );
  }

  Widget listViewBuilder() {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        var item = pageList[index];
        return InkWell(
          onTap: () {
            Get.toNamed(RouteGet.groupOrderInfo);
          },
          child: Container(
            margin: const EdgeInsets.only(
                top: Sizes.default20, left: Sizes.default20, right: Sizes.default20),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(Sizes.default10)),
            padding:
                const EdgeInsets.symmetric(vertical: Sizes.default10, horizontal: Sizes.default15),
            child: Column(
              children: [
                Container(
                  alignment: Alignment.centerLeft,
                  child: ListBody(
                    children: item.children
                        .asMap()
                        .map((key, child) => MapEntry(
                            key,
                            Container(
                              alignment: Alignment.centerLeft,
                              height: Sizes.default70,
                              margin: const EdgeInsets.only(bottom: Sizes.default15),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  GFImageOverlay(
                                    width: Sizes.default70,
                                    height: Sizes.default70,
                                    image: NetworkImage(child.imgUrl),
                                    borderRadius: BorderRadius.circular(Sizes.default5),
                                  ),
                                  Expanded(
                                      child: Container(
                                    padding: const EdgeInsets.only(left: Sizes.default15),
                                    height: Sizes.default70,
                                    child: Column(
                                      children: [
                                        Container(
                                          alignment: Alignment.centerLeft,
                                          child: Row(
                                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                            children: [
                                              Container(
                                                width: 130,
                                                alignment: Alignment.centerLeft,
                                                child: Row(
                                                  children: [
                                                    SizedBox(
                                                      width: 109,
                                                      child: Text(
                                                        child.name,
                                                        overflow: TextOverflow.ellipsis,
                                                        style: StylesUtils.customTextStyle(
                                                            fontSize: Sizes.default16,
                                                            color: ColorsUtils.color363),
                                                      ),
                                                    ),
                                                    Expanded(
                                                        child: Container(
                                                      alignment: Alignment.centerRight,
                                                      child: BrunoTools.getAssetSizeImage(
                                                          BrnAsset.iconRightArrow, 16, 16),
                                                    ))
                                                  ],
                                                ),
                                              ),
                                              if (key == 0)
                                                InkWell(
                                                  onTap: () {},
                                                  child: Text(
                                                    item.orderStatus,
                                                    style: StylesUtils.customTextStyle(
                                                        fontSize: Sizes.default15,
                                                        color: ColorsUtils.colorFf6),
                                                  ),
                                                ),
                                            ],
                                          ),
                                        ),
                                        Expanded(
                                            child: Container(
                                          alignment: Alignment.centerLeft,
                                          child: Text(
                                            child.statusText ?? '',
                                            style: StylesUtils.customTextStyle(
                                                color: '#FF9900', fontSize: Sizes.default12),
                                          ),
                                        )),
                                        Container(
                                          alignment: Alignment.centerLeft,
                                          child: Text(child.note ?? 'x ${child.num}',
                                              style: StylesUtils.customTextStyle(
                                                  color: child.note != null
                                                      ? ColorsUtils.color999
                                                      : ColorsUtils.color333,
                                                  fontSize: child.note != null
                                                      ? Sizes.default12
                                                      : Sizes.default14)),
                                        ),
                                      ],
                                    ),
                                  ))
                                ],
                              ),
                            )))
                        .values
                        .toList(),
                  ),
                ),
                SizedBox(
                  height: Sizes.default30,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        '预留手机',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color333, fontSize: Sizes.default13),
                      ),
                      Text(
                        '+86 ${item.mobile}',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color333, fontSize: Sizes.default13),
                      )
                    ],
                  ),
                ),
                SizedBox(
                  height: Sizes.default40,
                  child: DrawUnderLine(
                    dx: Sizes.default5,
                    color: ColorsUtils.parseColorFromHexString('#D7D7D7'),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '应付款',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color333, fontSize: Sizes.default13),
                        ),
                        Text(
                          '￥ ${item.meetPay}',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color333,
                              fontSize: Sizes.default13,
                              fontWeight: Fonts.w7),
                        )
                      ],
                    ),
                  ),
                ),
                Container(
                  height: Sizes.default40,
                  alignment: Alignment.centerRight,
                  child: Text.rich(TextSpan(
                      text: '本单预计收入：',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color333, fontSize: Sizes.default13),
                      children: [
                        TextSpan(
                            text: '￥${item.income}',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.colorFf0,
                                fontWeight: Fonts.w7,
                                fontSize: Sizes.default13))
                      ])),
                )
              ],
            ),
          ),
        );
      },
      itemCount: pageList.length,
    );
  }

  void onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
    setState(() {});
  }

  void onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 15) {
      return refreshController.loadNoData();
    }

    /// 添加订单
    refreshController.loadComplete();
    pageList.add(_GroupOrderModel(
        orderStatus: '关闭交易',
        mobile: '135998476'.replaceRange(3, 8, '*'),
        meetPay: 875.04,
        income: 670.01,
        children: [
          _OrderInfoModel(
              imgUrl:
                  'https://img1.baidu.com/it/u=3033226715,2238019049&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
              name: '烤肉两人套餐烤肉两人套餐烤肉两人套餐烤肉两人套餐')
        ]));
    setState(() {});
  }
}

class _GroupOrderModel {
  /// 订单状态
  String orderStatus;

  /// 手机
  String mobile;

  /// 应付款
  double meetPay;

  /// 收入
  double income;

  List<_OrderInfoModel> children;

  _GroupOrderModel(
      {required this.orderStatus,
      required this.mobile,
      required this.meetPay,
      required this.income,
      this.children = const []});
}

class _OrderInfoModel {
  /// 图片地址
  String imgUrl;

  /// 名称
  String name;

  /// 状态文本
  String? statusText;

  /// 备注
  String? note;

  /// 数量
  int num;

  _OrderInfoModel(
      {required this.imgUrl, required this.name, this.statusText, this.note, this.num = 1});
}
